﻿using Uniforax.Core.Common;

namespace Uniforax.Core.GameActions.GridMove
{
    public class GridPlaceAction : BaseGridMoveAction
    {
        public PosId PlacePosId { get; set; }

        public override void Execute(ActionContext context)
        {
            var world = context.GameWorld;
            var grid = world.GetGrid(PlacePosId);
            var gameObject = world.GetRestoredTempGameObject(ObjectId);

            var placeContext = new GameObjectPlaceGridMovementContext()
            {
                GameObject = gameObject,
                CurrentGrid = grid,
            };

            grid.PlaceObject(gameObject);
            world.RemoveRestoredTempGameObject(gameObject);

            grid.OnGameObjectPlaced(context, placeContext);
            gameObject.OnPlacedGrid(context, placeContext);
        }

        public override void Undo(ActionContext context)
        {
            var world = context.GameWorld;
            var grid = world.GetGrid(PlacePosId);
            var gameObject = grid.GetPlacedObject(ObjectId);
            grid.RemovePlacedObject(gameObject);
            world.RestoreTempGameObject(gameObject, ObjectId);
        }
    }
    /*
     * var world = context.GameWorld;

            var grid1 = world.GetGrid(FromPosId);

            var objectId = ObjectId;

            var gameObject = grid1.GetPlacedObject(objectId);
            if (gameObject is null)
            {
                throw new NullReferenceException($"MoveStepGameObjectAction.Execute: 无法在 {grid1} 找到 Id 为 {objectId} 的对象。");
            }

            // leave
            var leaveContext = new GameObjectLeaveGridMovementContext()
            {
                GameObject = gameObject,
                OldGrid = grid1,
            };

            grid1.RemovePlacedObject(gameObject);
            grid1.OnGameObjectLeave(context, leaveContext);

            if (leaveContext.IsCancelled == true) { IsCancenlled = true; return; }

            gameObject.OnLeaveGrid(context, leaveContext);
            if (leaveContext.IsCancelled == true) { IsCancenlled = true; return; }

            // enter
            var grid2 = world.GetGrid(ToPosId);

            var enterContext = new GameObjectEnterGridMovementContext()
            {
                GameObject = gameObject,
                NewGrid = grid2,
            };

            grid2.OnGameObjectEnter(context, enterContext);
            if (enterContext.IsCancelled == true) { IsCancenlled = true; return; }
            gameObject.OnEnterGrid(context, enterContext);
            if (enterContext.IsCancelled == true) { IsCancenlled = true; return; }

            // place
            var placeContext = new GameObjectPlaceGridMovementContext()
            {
                GameObject = gameObject,
                CurrentGrid = grid2,
            };

            grid2.PlaceObject(gameObject);
            grid2.OnGameObjectPlaced(context, placeContext);
            if (placeContext.IsCancelled == true) { IsCancenlled = true; return; }
            gameObject.OnPlacedGrid(context, placeContext);
            if (placeContext.IsCancelled == true) { IsCancenlled = true; return; }

     */
}
